module.exports = (EdtApi) => {
    "use strict";
    let chatId = "", count = 0;
    function isGroup() {
        return false;
    }
    async function getChatId(times = 0) {
        let url = window.location.href;
        const regex = /\/messaging\/thread\/([^\/?#]+)/;
        const match = regex.exec(url);
        let id = '';
        try {
            if (match) id = match[1];
        } catch (error) {
            console.log('获取对话id失败', error)
        }
        if (id && id != chatId) return id;
        await EdtApi.sleep(50);
        if (times > 10) return id;
        times++;
        return getChatId(times);
    }
    return {
        config: {
            friendList: '#main ul.msg-conversations-container__conversations-list,#global-nav nav>ul>li',
            chat: '#main div.msg__detail',
            input: '#main div.msg__detail form.msg-form div[role="textbox"]',
            msgBox: '#main div.msg__detail div.msg-s-message-list ul.msg-s-message-list-content',
            inMsg: 'li.msg-s-message-list__event>div.msg-s-event-listitem.msg-s-event-listitem--other div.msg-s-event-listitem__obfuscated-message-card:not(.trans-inited)',
            outMsg: 'li.msg-s-message-list__event>div.msg-s-event-listitem:not(.msg-s-event-listitem--other) div.msg-s-event__content>p:not(.trans-inited)',
            sendBtn: '#main div.msg__detail form.msg-form footer div.msg-form__right-actions button.msg-form__send-button[type="submit"]',
            setting: "#main div.msg__detail form.msg-form footer div.msg-form__right-actions",
            tips: {
                in: '#main div.msg-title-bar dl.truncate',
                out: '#main div.msg__detail form.msg-form'
            },
            crm: {
                // listener: '#LeftColumn-main',
                top: '#main div.msg-title-bar dl.truncate>dt',
                left: ""
            },
            placeholder: {
                selector: "#main div.msg__detail form.msg-form div.msg-form__placeholder",
                attr: "data-placeholder"
            },
            autoCount: 20,
        },
        getUnread() {
            count = 0;
            return count;
        },
        getChatInfo() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                resolve({ id: chatId, isGroup: isGroup() });
            });
        },
        getChatTitle() {
            let titleEl = document.querySelector('#main div.msg-title-bar dl.truncate>dt>h2');
            return titleEl?.textContent?.trim() || "";
        },
        getText(msgBox) {
            if (!msgBox) return "";
            let text = '';
            try {
                msgBox.childNodes?.forEach((child) => {
                    if (child.nodeName == "#text") {
                        text += child.textContent || "";
                    } else if (child.nodeName == "BR") {
                        text += "\n";
                    }
                });
            } catch (error) {
                console.error(error);
            }
            return text;
        },
        getIptMsg(inputEl) {
            if (!inputEl) return "";
            let value = "", arr = [];
            try {
                let ps = inputEl.querySelectorAll('p');
                ps?.forEach((p) => {
                    arr.push(p.textContent || "");
                });
                value = arr.join("\n");
            } catch (error) {
                console.error(error);
            }
            return value;
        },
        getUserInfo() {
            try {
                let avatarEl = document.querySelector('#global-nav nav>ul>li div.global-nav__me img.global-nav__me-photo');
                let userName = avatarEl?.getAttribute("alt") || "";
                let avatar = avatarEl?.getAttribute("src") || "";
                let account = userName;
                return {
                    avatar,
                    userName,
                    account
                };
            } catch (error) {
                return null;
            }
        }
    }
};